
package com.workcell.workdata.xthings.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.constant.ValveStateEnum;
import com.workcell.workdata.xthings.entity.BaseValve;
import com.workcell.workdata.xthings.entity.CodeChainnum;
import com.workcell.workdata.xthings.entity.CodeValve;
import com.workcell.workdata.xthings.mapper.CodeValveMapper;
import com.workcell.workdata.xthings.service.BaseValveService;
import com.workcell.workdata.xthings.service.CodeValveService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 码库液位计信息表
 *
 * @author pigx code generator
 * @date 2023-05-16 13:58:21
 */
@Service
public class CodeValveServiceImpl extends ServiceImpl<CodeValveMapper, CodeValve> implements CodeValveService {

    @Resource
    private BaseValveService baseValveService;

    /**
     * 保存液位计二维码基本快照信息
     * @param codeChainnum
     * @return
     */
	@Override
    public Boolean saveCodeBaseInfo(CodeChainnum codeChainnum) {
        // 查询硬件信息
        LambdaQueryWrapper<BaseValve> baseValveCondition = new QueryWrapper<BaseValve>().lambda()
                .eq(BaseValve::getId,codeChainnum.getBaseId());
        BaseValve baseValve = baseValveService.getOne(baseValveCondition);
        if (ObjectUtil.isEmpty(baseValve)){
            return false;
        }
        // 添加二维码快照信息
        CodeValve saveBean = new CodeValve();
        BeanUtil.copyProperties(baseValve,saveBean, "createBy","createTime","updateBy","updateTime");
        saveBean.setCodeId(codeChainnum.getCodeId());
        saveBean.setChainNum(codeChainnum.getChainNum());
        saveBean.setDeviceNo(codeChainnum.getDeviceNo());
        return this.save(saveBean);
    }

    /**
     * 获取硬件清单
     */
	@Override
    public List<JSONObject> getHardwareList() {
		LambdaQueryWrapper<BaseValve> lambdaQueryWrapper = Wrappers.lambdaQuery(BaseValve.class);
		lambdaQueryWrapper.eq(BaseValve::getState, HardwareStateEnum.INUSE.getCode());
        return baseValveService.list(lambdaQueryWrapper).stream().map(i -> {
            JSONObject resultBean = new JSONObject();
            resultBean.put("baseId",i.getId());
            resultBean.put("manufacturer",i.getManufacturer());
            resultBean.put("hardwareName",i.getName());
            return resultBean;
        }).collect(Collectors.toList());
    }


    /**
     * 查询硬件基本信息
     * @param baseIds
     * @return
     */
	@Override
    public Object getHardwareBase(String baseIds) {
        List<String> baseList = Arrays.asList(baseIds.split(","));
        LambdaQueryWrapper<BaseValve> baseValveCondition = new QueryWrapper<BaseValve>().lambda()
                .in(CollectionUtil.isNotEmpty(baseList),BaseValve::getId,baseList);
        List<BaseValve> baseValveList = baseValveService.list(baseValveCondition);
        return baseValveList;
    }


    /**
     * 查询硬件快照信息
     */
	@Override
    public Object getHardwareInfo(String codeId) {
		CodeValve codeValve = this.getById(codeId);
		codeValve.setState(ValveStateEnum.getByCode(codeValve.getState()).getMsg());
		return codeValve;
    }


	@Override
	public Boolean updateHardwareStatus(String deviceNo, String manufacturerCode, String status) {
		LambdaUpdateWrapper<CodeValve> lambdaUpdateWrapper = Wrappers.lambdaUpdate(CodeValve.class)
				.eq(CodeValve::getDeviceNo, deviceNo)
				.eq(ObjectUtil.isNotNull(manufacturerCode),CodeValve::getManufacturerCode, manufacturerCode)
				.set(CodeValve::getState, status);
		return this.update(lambdaUpdateWrapper);
	}

	@Override
	public Object getHardwareBaseOpen(String deviceNo) {
		LambdaQueryWrapper<CodeValve> lambdaQueryWrapper = Wrappers.lambdaQuery(CodeValve.class);
		lambdaQueryWrapper.eq(CodeValve::getDeviceNo,deviceNo);
		CodeValve codeValve = this.getOne(lambdaQueryWrapper);
		if (ObjectUtil.isNotNull(codeValve)){
			codeValve.setState(ValveStateEnum.getByCode(codeValve.getState()).getMsg());
		}
		return codeValve;
	}
}
